package cn.zshprint.serein.service.impl;

import cn.zshprint.serein.config.RedisConfig;
import cn.zshprint.serein.entity.Product;
import cn.zshprint.serein.mapper.ProductMapper;
import cn.zshprint.serein.pojo.EsProduct;
import cn.zshprint.serein.repository.EsProductRepository;
import cn.zshprint.serein.service.EsProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * impl es产品服务
 *
 * @author zshprint
 * @date 2021/07/29
 */
@Service
public class EsProductServiceImpl implements EsProductService {
    /**
     * 产品映射器
     */
    @Resource
    private ProductMapper productMapper;
    /**
     * es产品库
     */
    @Resource
    private EsProductRepository esProductRepository;


    /**
     * 导入所有
     *
     * @return int
     */
    @Override
    public int importAll() {
        List<EsProduct> list = new ArrayList<>();
        List<Product> products = productMapper.selectList(null);
        for (Product product : products) {
            EsProduct item = new EsProduct();
            BeanUtils.copyProperties(product, item);
            list.add(item);
        }
        Iterable<EsProduct> esProducts = esProductRepository.saveAll(list);
        Iterator<EsProduct> iterator = esProducts.iterator();
        int result = 0;
        while (iterator.hasNext()) {
            result++;
            iterator.next();
        }
        return result;
    }

    /**
     * 删除
     *
     * @param id id
     */
    @Override
    public void delete(Long id) {
        esProductRepository.deleteById(id);
    }

    /**
     * 创建
     *
     * @param id id
     * @return {@link EsProduct}
     */
    @Override
    public EsProduct create(Long id) {
        Product product = productMapper.selectById(id);
        EsProduct esProduct = new EsProduct();
        BeanUtils.copyProperties(product, esProduct);
        return esProductRepository.save(esProduct);
    }

    /**
     * 删除
     *
     * @param ids id
     */
    @Override
    public void delete(List<Long> ids) {
        if (!CollectionUtils.isEmpty(ids)) {
            List<EsProduct> esProductList = new ArrayList<>();
            for (Long id : ids) {
                EsProduct esProduct = new EsProduct().setId(id.intValue());
                esProductList.add(esProduct);
            }
            esProductRepository.deleteAll(esProductList);
        }
    }

    /**
     * 搜索
     *
     * @param keyword 关键字
     * @return {@link List<EsProduct>}
     */
    @Override
    public List<EsProduct> search(String keyword) {
        return esProductRepository.findByName(keyword);
    }

    @Override
    public List<EsProduct> searchAll() {
        Iterable<EsProduct> all = esProductRepository.findAll();
        all.forEach(item -> System.out.println(item));
        List<EsProduct> res = new ArrayList<>();
        all.forEach(item -> res.add(item));
        return res;
    }

    @Override
    public Integer save(Product product) {
        EsProduct esProduct = new EsProduct();
        BeanUtils.copyProperties(product, esProduct);
        EsProduct save = esProductRepository.save(esProduct);
        return save.getId();
    }

    @Override
    public EsProduct update(Product product) {
        EsProduct esProduct = new EsProduct();
        BeanUtils.copyProperties(product, esProduct);
        esProductRepository.deleteById(((long) product.getId()));
        EsProduct save = esProductRepository.save(esProduct);
        return save;
    }
}
